package lee.vioson.ebook.ui.components

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.layout.width
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import com.laiyifen.uikit.isNotEmpty
import lee.vioson.ebook.ui.AppTheme

/**
 *Author:viosonlee
 *Date:2023/6/12
 *DESCRIPTION:
 */
@Composable
fun TopBar(
    modifier: Modifier = Modifier,
    title: String? = null,
    navHostController: NavHostController? = null,
    onBack: (() -> Unit)? = null,
    action: @Composable (() -> Unit)? = null
) {
    Column(
        modifier = Modifier
            .then(modifier)
            .fillMaxWidth()
    ) {
        Row(
            Modifier
                .fillMaxWidth()
                .statusBarsPadding()
                .height(48.dp)
        ) {
            Box(
                modifier = Modifier
                    .fillMaxHeight()
                    .width(48.dp)
                    .clickable {
                        if (onBack == null) {
                            navHostController?.popBackStack()
                        } else {
                            onBack()
                        }
                    }, contentAlignment = Alignment.Center
            ) {
                Icon(
                    imageVector = Icons.Default.ArrowBack,
                    contentDescription = "back icon",
                    tint = AppTheme.colors.onBackground
                )
            }
            Box(
                modifier = Modifier
                    .weight(1f)
                    .fillMaxHeight(), contentAlignment = Alignment.Center
            ) {
                if (title.isNotEmpty) {
                    Text(text = title!!, style = AppTheme.titleStyle)
                }
            }
            Box(
                modifier = Modifier
                    .fillMaxHeight()
                    .width(48.dp)
            ) {
                if (action != null) {
                    action()
                }
            }
        }
        Spacer(
            modifier = Modifier
                .height(1.dp)
                .fillMaxWidth()
                .background(AppTheme.colors.onBackground)
        )
    }
}

@Preview
@Composable
fun TopBarPreview() {
    TopBar(title = "title")
}